package com.boot4g.admin.module.publish.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.boot4g.admin.module.publish.service.PushSqlService;
import com.boot4g.entity.PushSqlEntity;
import com.boot4g.utils.PageUtils;
import com.boot4g.utils.Query;
import com.boot4g.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.*;


/**
 *
 *
 * @author chenjinjie
 * @email guangyaoliangzijie@163.com
 * @date 2018-08-16 14:44:17
 */
@RestController
@RequestMapping("sys/pushsql")
public class PushSqlController {
	@Autowired
	private PushSqlService pushSqlService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		if (!org.springframework.util.StringUtils.isEmpty(query.get("pushPre"))) {
			query.put("pushPre", Integer.valueOf(Integer.parseInt(query.get("pushPre").toString())));
		} else {
			query.remove("pushPre");
		}

        Page<PushSqlEntity> page = pushSqlService.selectByPage(query.getPagination(),query);

        PageUtils pageUtil = new PageUtils(page.getRecords(), page.getTotal(), query.getLimit(), query.getCurrPage());

		return R.ok().put("page", pageUtil);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{rid}")
	public R info(@PathVariable("rid") Integer rid){
		PushSqlEntity pushSql = pushSqlService.selectById(rid);

		return R.ok().put("pushSql", pushSql);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody PushSqlEntity pushSql){
		pushSql.setCreateTm(new Date());
		pushSqlService.insert(pushSql);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody PushSqlEntity pushSql){
		pushSql.setUpdateTm(new Date());
		pushSqlService.updateById(pushSql);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] rids){
		pushSqlService.deleteBatchIds(Arrays.asList(rids));

		return R.ok();
	}
	/**
	 * 执行测试
	 */
	@RequestMapping("/push_pre")
	public R push_pre(@PathParam("executor") String executor, @PathParam("executorTag") String executorTag, @RequestBody Integer[] rids){
		List<PushSqlEntity> pushSqlEntities = new ArrayList<PushSqlEntity>();
		for (Integer rid : rids) {
			PushSqlEntity pushSqlEntity = new PushSqlEntity();
			pushSqlEntity.setRid(rid);
			pushSqlEntity.setPushPre(1);
			if(StringUtils.isNotEmpty(executor)) {
				pushSqlEntity.setExecutor(executor);
			}
			if(StringUtils.isNotEmpty(executorTag)) {
				pushSqlEntity.setExecuteTag(executorTag);
			}
			pushSqlEntities.add(pushSqlEntity);
		}
		pushSqlService.updateBatchById(pushSqlEntities);
		return R.ok();
	}

	
}
